package com.summer.main;

import java.math.BigDecimal;

public class ClacTest {
	
	public static void main(String[] args) {
		
		long m = 89;
		long e = 3;
		long n = 3127;
		long d = 2011;
		long c = ((long) Math.pow(m, e)) % n;
		long r = ((long) Math.pow(c, d)) % n;
		System.out.println("原文："+m+"密文："+c+"结果："+((long) Math.pow(c, d)));
		
		
		
		System.out.println("clac === " + (2 << 9));
		System.out.println("clac === " + 4028 / 1024);
		System.out.println("clac === " + (4028 >> 10));

		
		System.out.println("clac === " + formatStorageSize(10000100));
		System.out.println("clac === " + formatStorageSize1(10000100));
		System.out.println("clac === " + formatStorageSize2(10000100));
		
		System.out.println("clac ===1 " + formatStorageSize(1024000));
		System.out.println("clac ===1 " + formatStorageSize1(1024000));
		System.out.println("clac ===1 " + formatStorageSize2(1024000));
		
		
		System.out.println("clac === " + formatStorageSize(100));
		System.out.println("clac === " + formatStorageSize1(100));
		System.out.println("clac === " + formatStorageSize2(100));
		
		
		String timeFormat = "0000-00-00 23:59:59";
        System.out.println(replace("2020-12-01 13", timeFormat, 0, timeFormat.length()));
        System.out.println(replace("2020-12-01 13", timeFormat, 0, 0));

        System.out.println(replace("2020-12-01 13 33333333333333333333", timeFormat, 0, timeFormat.length()));
        System.out.println(replace("2020-12-01 13 33333333333333333333", timeFormat, 0, 0));
        
        System.out.println(replace("2020-12-01 13 ", timeFormat, 10, timeFormat.length()));
        System.out.println(replace("2020-12-01 13 ", timeFormat, 10, 0));
		
        
        StringBuilder titleBuilder = new StringBuilder();
        boolean isFirst = true;
        String[] book_name_list = new String[]{"女巨人也要谈恋爱","恶女甜妻不好惹"};
        for(String bookName : book_name_list) {
            titleBuilder.append(isFirst ? "" : "+");
            titleBuilder.append(String.format("《%s》", bookName));
            isFirst = false;
        }
        CharSequence title = replace("...》", titleBuilder.toString(), 13, 17);
        
        System.out.println(title);
        
	}
	

	public static String replace(String src, String dst, int start, int cutStart) {
		if(start >= dst.length()) {
			return dst;
		}
		
        int end = start + src.length();
        StringBuilder builder = new StringBuilder(dst);
        builder.replace(start, end, src);

        if(cutStart > 0 && cutStart < builder.length()) {
            return builder.substring(0, cutStart);
        }

        return builder.toString();
    }
	
    
    public static String formatStorageSize(long storageSize) {
    	// 内存机制权值 1024 ， 为了计算小数固位Double
        double weight = 1024d;
        // 进制单位
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        // 格式化后小数大小
        double byteSize = storageSize;
        // 格式化结果
        String format = storageSize + units[0];
        
        for(String unit : units) {
        	format = new BigDecimal(byteSize).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
            format = subZeroAndDot(format) + unit;
        	byteSize = byteSize / weight;
            if(byteSize < 1) {
            	break;
            }
        }
        
        return format;
    }
    
    public static String subZeroAndDot(String s){
        if(s.indexOf(".") > 0){
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }
    
    public static String formatStorageSize1(long storageSize) {
        long kiloByte = storageSize >> 10;
        if (kiloByte < 1) {
            return storageSize + "B";
        }

        long megaByte = kiloByte >> 10;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        long gigaByte = megaByte >> 10;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        long teraBytes = gigaByte >> 10;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);

        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }
    
    public static String formatStorageSize2(long storageSize) {

        double kiloByte = storageSize / 1024d;
        if (kiloByte < 1) {
            return storageSize + "B";
        }

        double megaByte = kiloByte / 1024d;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024d;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024d;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);

        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";

    }
	
}
